package com.gbx.sportsnews;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.gbx.sportsnews.fragments.HeadlineFragment;
import com.gbx.sportsnews.fragments.MatchFragment;
import com.gbx.sportsnews.fragments.NewsFragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private RadioGroup radioGroup;
    private List<Fragment> list;
    private NewsFragment newsFragment;
    private HeadlineFragment headlineFragment;
    private MatchFragment matchFragment;
    private FragmentManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup = (RadioGroup) findViewById(R.id.main_radio_group);
        radioGroup.setOnCheckedChangeListener(this);
        newsFragment=new NewsFragment();
        headlineFragment = new HeadlineFragment();
        matchFragment = new MatchFragment();
        list=new ArrayList<>();
        list.add(newsFragment);
        list.add(headlineFragment);
        list.add(matchFragment);
        manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        for (int i = 0; i <list.size() ; i++) {
            transaction.add(R.id.main_container, list.get(i));
        }
        transaction.commit();
        showFragment(0);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.main_rg_news:
                showFragment(0);
                break;
            case R.id.main_rg_headline:
                showFragment(1);
                break;
            case R.id.main_rg_match:
                showFragment(2);
                break;
        }
    }
    public void showFragment(int position){
        FragmentTransaction transaction = manager.beginTransaction();
        for (int i = 0; i < list.size(); i++) {
            if (position==i){
                transaction.show(list.get(i));
            }else {
                transaction.hide(list.get(i));
            }
        }
        transaction.commit();
    }

    //返回键双击退出
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK){
            exitBy2Click();
        }
        return false;
    }
    //双击退出
    private boolean isExit=false;
    private void exitBy2Click(){
        Timer tExit=null;
        if (isExit==false){
            isExit=true;//准备退出
            Toast.makeText(this,"再按一次退出体育头条",Toast.LENGTH_SHORT).show();
            tExit=new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit=false;//取消退出
                }
            },2000);//如果2秒没有按下返回键，则定时器取消刚才执行的任务
        }else {
            finish();
            System.exit(0);
        }
    }
}
